﻿/**
*基本全局变量
*/
var mode = true;  //mode为true表示距离,false表示面积
//注意，测距与测面积不可同时进行
var isDistanceClick = false;
var isAreaClick = false;

var Polylines = []; //测距线集合
var Icons1 = [];
var Polygons = []; //测面集合
var Icons2 = [];

/**
*点击测距按钮事件
*/
function distanceClick() {

    if (isAreaClick)  //如果正在测面积
        document.getElementById("Errortips").innerHTML = "提示信息 : 请先退出测面积模式，然后再切换到测距模式";
    else {
        isDistanceClick = !isDistanceClick;

        if (isDistanceClick)  //点击的时候
        {
            document.getElementById("btnDistance").innerHTML = "取消";
            document.getElementById("btnDistance").style.fontWeight = 'bold';
            document.getElementById("DistanceInfo").style.display = 'block';
            document.getElementById("Errortips").innerHTML = "提示信息 : 进入测距模式，退出请点击取消";
            mode = true;

            dMap.setMouseMode(DMouseMode.DRAW_POLYLINE,
            //回调函数
								function(strCoord) {
								    /**
								    *回调回来的是线经纬度坐标串，用户用来定义先对象，添加到地图上
								    **/
								    dMap.setMouseMode(DMouseMode.MAP_DRAG);
								    //线对象属性
								    var draw_graphic_style = { color: "red", weight: 3, opacity: 0.9 };
								    //定义先对象
								    pl = new DPolyline(strCoord, draw_graphic_style);
								    Polylines.push(pl);
								    //地图添加线对象
								    dMap.addOverlay(pl);
								    pl.enableEditing();
								    //字符串转数组
								    dstrCoord = strCoord.split(",");
								    var lon = new Array();
								    var lat = new Array();
								    var len = 0;
								    //求线段的距离
								    for (i = 0; i < (dstrCoord.length / 2); i++) {

								        //								        lon[i] = parseFloat(dstrCoord[2 * i]) + 0.05;
								        //								        lat[i] = parseFloat(dstrCoord[(2 * i + 1)]) - 0.17;
								        lon[i] = parseFloat(dstrCoord[2 * i]);
								        lat[i] = parseFloat(dstrCoord[(2 * i + 1)]);
								        var cc = new DLonLat(lon[i], lat[i]);
								        var icon = new DIcon(cc);
								        Icons1.push(icon);
								        dMap.addOverlay(icon);
								        if (i > 0) {
								            pt1 = new DLonLat(lon[i - 1], lat[i - 1]);
								            pt2 = new DLonLat(lon[i], lat[i]);
								            var d = DLonLat.getDistanceOfTwo(pt1, pt2);
								            len = d + len;
								            dresult = Math.round(len) / 1000;
								            document.getElementById("DistanceInfo").innerHTML = "距离: " + dresult + "公里";
								        }
								    }

								});
        }
        else {
            document.getElementById("btnDistance").innerHTML = "测距";
            document.getElementById("btnDistance").style.fontWeight = 'normal';
            document.getElementById("DistanceInfo").style.display = 'none';
            document.getElementById("Errortips").innerHTML = "";
            document.getElementById("DistanceInfo").innerHTML = "距离:";

            for (var i = 0; i < Icons1.length; i++) {
                dMap.removeOverlay(Icons1[i]);
            }
            for (var j = 0; j < Polylines.length; j++) {
                dMap.removeOverlay(Polylines[j]);
            }
            //            dMap.clearOverlays();


        }
    }

}

/**
* 点击测面积
*/
function AreaClick() {
    if (isDistanceClick)  //如果正在测距
        document.getElementById("Errortips").innerHTML = "提示信息 : 请先退出测距模式，然后再切换到测面积模式";
    else {
        isAreaClick = !isAreaClick;

        if (isAreaClick)  //点击的时候
        {
            document.getElementById("btnArea").innerHTML = "取消";
            document.getElementById("btnArea").style.fontWeight = 'bold';
            document.getElementById("AreaInfo").style.display = 'block';
            document.getElementById("Errortips").innerHTML = "进入测面积模式，退出请点击取消";
            mode = false;
            dMap.setMouseMode(DMouseMode.DRAW_POLYGON,
            //回调函数
								function(strCoord) {
								    /**
								    *回调回来的是线经纬度坐标串，用户用来定义先对象，添加到地图上
								    **/
								    dMap.setMouseMode(DMouseMode.MAP_DRAG);
								    var polygon = null;
								    if (!polygon) {
								        //定义多边形对象
								        polygon = new DPolygon(strCoord, { color: "red", weight: 3, opacity: 1 });
								        Polygons.push(polygon);
								        //添加多边形对象到地图上
								        dMap.addOverlay(polygon);
								    }
								    //地图添加线对象
								    dstrCoord = strCoord.split(",");
								    var lon = new Array();
								    var lat = new Array();
								    var len;
								    for (i = 0; i < dstrCoord.length / 2 - 1; i++) {
								        //								        lon[i] = parseFloat(dstrCoord[2 * i]) + 0.05;
								        //								        lat[i] = parseFloat(dstrCoord[(2 * i + 1)]) - 0.17;
								        lon[i] = parseFloat(dstrCoord[2 * i]);
								        lat[i] = parseFloat(dstrCoord[(2 * i + 1)]);
								        var cc = new DLonLat(lon[i], lat[i]);
								        var icons = new DIcon(cc);
								        Icons2.push(icons);
								        dMap.addOverlay(icons);
								    }
								    //求多边形面积
								    var s = 0.0;
								    var t;
								    var a;
								    var b;
								    var c;
								    var temp;
								    for (i = 0; i < (dstrCoord.length / 2 - 3); i++) {

								        pt1 = new DLonLat(lon[0], lat[0]);
								        pt2 = new DLonLat(lon[i + 1], lat[i + 1]);
								        pt3 = new DLonLat(lon[i + 2], lat[i + 2]);
								        a = DLonLat.getDistanceOfTwo(pt1, pt2);
								        b = DLonLat.getDistanceOfTwo(pt1, pt3);
								        c = DLonLat.getDistanceOfTwo(pt2, pt3);
								        a = Math.round(a) / 1000;
								        b = Math.round(b) / 1000;
								        c = Math.round(c) / 1000;
								        t = (a + b + c) / 2;
								        temp = Math.sqrt(t * (t - a) * (t - b) * (t - c));
								        s += temp;
								        s = Math.round(s * 1000) / 1000;


								        document.getElementById("AreaInfo").innerHTML = "面积:" + s + "平方公里";
								    }
								});
        }
        else {
            document.getElementById("btnArea").innerHTML = "测面积";
            document.getElementById("btnArea").style.fontWeight = 'normal';
            document.getElementById("AreaInfo").style.display = 'none';
            document.getElementById("Errortips").innerHTML = "提示信息 : ";
            document.getElementById("AreaInfo").innerHTML = "面积:";

            //            dMap.clearOverlays();
            for (var i = 0; i < Icons2.length; i++) {
                dMap.removeOverlay(Icons2[i]);
            }
            for (var j = 0; j < Polygons.length; j++) {
                dMap.removeOverlay(Polygons[j]);
            }

            //            for (iii = 0; iii < 2; iii++) {
            //                distanceClick();
            //            }
        }
    }
}


function formatDistance(len) {
    len = Math.round(len);
    if (len <= 1000) {
        return len + ' ' + "米";
    } else if (len <= 1000000) {
        return len / 1000 + ' ' + "公里";
    }
    return Math.round(len / 1000) + ' ' + "公里";
}


	
